import { useEffect, useState } from 'react';

export const isFalsy = (value: any) => (value === 0 ? false : !value);

export const cleanObject = (object: any) => {
  const result = { ...object };
  Object.keys(result).forEach((key) => {
    const value = result[key];
    if (isFalsy(value)) {
      delete result[key];
    }
  });
  return result;
};

export const useDebounce = <T>(value: T, delay?: number) => {
  const [debouncedPerson, setDebouncedPerson] = useState(value);
  useEffect(() => {
    const timeOut = setTimeout(() => setDebouncedPerson(value), delay);
    return () => clearTimeout(timeOut);
  }, [value]);
  return debouncedPerson;
};
